<?php

namespace App\Http\Middleware;

use Illuminate\Support\Facades\Log;
use Closure;

class LaravelLogs
{

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        try {
            Log::channel('daily')->info('this is operation info :', ['用户id' => $request->user()->id, '用户姓名' => $request->user()->username,
                'ip' => real_ip(), '参数' => $request->all(), '方式' => $request->getRealMethod(),
                '域名' => $request->getHttpHost(), '地址' => $request->getRequestUri(), '控制器方法' => $request->route()->getActionName(),
                '端口' => $request->getPort()
            ]);
        } catch (\Exception $e) {
            if($e->getCode() !== 0){
                Log::channel('errordaily')->error('this is a error happened:', ['错误码' => $e->getCode(), '错误信息' => $e->getMessage()]);
            }
        }

        return $response;
    }

}
